package com.tencent.qf.team.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tencent.qf.common.redis.RedisService;
import com.tencent.qf.team.dao.TySceneChannelDao;
import com.tencent.qf.team.dto.response.TySceneChannelDto;
import com.tencent.qf.team.service.SceneChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SceneChannelServiceImpl implements SceneChannelService {

    private final static String sceneChannelListCacheKey = "tdwApi-sceneChannelListCacheKey";
    @Autowired
    private RedisService redisService;

    @Autowired
    private TySceneChannelDao tySceneChannelDao;

    /**
     * 成交渠道列表
     *
     * @return
     */
    @Override
    public List<TySceneChannelDto> sceneChannelList() {
        Object cache = redisService.getValue(sceneChannelListCacheKey);
        if (cache != null) {
            return JSON.parseObject(cache.toString(), new TypeReference<List<TySceneChannelDto>>() {
            });
        }
        List<TySceneChannelDto> data = tySceneChannelDao.selectAll();
        if (!CollectionUtils.isEmpty(data)) {
            redisService.setValue(sceneChannelListCacheKey, JSON.toJSONString(data), 24 * 60 * 60L, TimeUnit.SECONDS);
        }
        return data;
    }
}
